package com.xiaobai.aroundtest.controller;

import com.xiaobai.aroundtest.entity.Article;
import com.xiaobai.aroundtest.service.IAroundTestService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangtw
 * @date 2023/12/6 0:20
 * @description 切面测试控制器
 */
@Slf4j
@AllArgsConstructor
@RestController
public class AroundTestController {

    private final IAroundTestService aroundTestService;

    @PostMapping("aroundTest")
    public void aroundTest(@RequestParam String name, @RequestBody Article article) {
        aroundTestService.aroundTest(name, article);
    }

    @GetMapping("/getRequestByParam")
    public Map<String, Object> getRequestByParam(@RequestParam("name") String name) {
        log.info("名称：" + name);

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

    @GetMapping("/getRequestByPlaceHolder/{name}/{age}")
    public Map<String, Object> getRequestByPlaceHolder(@PathVariable("name") String name,
                                                       @PathVariable("age") String age) {
        log.info("名称：" + name);
        log.info("年龄：" + age);

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

    @PostMapping("/postRequest")
    public Map<String, Object> postRequest(@RequestParam String name, @RequestBody Article article) {
        log.info("名称：" + name);

        log.info("文章名称：" + article.getName());
        log.info("文章描述：" + article.getDescription());

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

    @PostMapping("/postRequestHeader")
    public Map<String, Object> postRequestHeader(HttpServletRequest request,
                                                 @RequestParam String name, @RequestBody Article article) {

        String token = request.getHeader("token");
        log.info("请求token：" + token);

        log.info("名称：" + name);

        log.info("文章名称：" + article.getName());
        log.info("文章描述：" + article.getDescription());

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

    @PostMapping("/upload")
    public Map<String, Object> upload(@RequestParam String name, MultipartFile uploadFile) throws IOException {
        log.info("名称：" + name);

        uploadFile.transferTo(new File("D:\\temp/" + uploadFile.getOriginalFilename()));

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

    @PostMapping("/download")
    public Map<String, Object> download(@RequestParam String fileName,
                                        HttpServletResponse response) {
        log.info("文件名称：" + fileName);

        File file = new File("D:\\temp/" + fileName);

        try(FileInputStream fileInputStream = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream()) {

            response.setHeader("content-disposition","attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8"));

            FileCopyUtils.copy(fileInputStream, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

    @GetMapping(value = "/getString")
    public String getString() {
        return "操作成功";
    }


}
